package org.cunamas.core.dao.impl;

import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.cunamas.core.bean.BudgetInputBean;
import org.cunamas.core.dao.PresupuestoDao;
import org.cunamas.web.util.CunamasConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;


@Component
public class PresupuestoDaoJdbcImpl implements PresupuestoDao {
	
	private JdbcTemplate jdbcTemplate;
	
	private final static String SQL_DELETE_PRESUPUESTO_PROGRAMADO = "delete from dbo."+ CunamasConstants.TABLA_PRESUPUESTO_PROGRAMADO +" where ANIO = ? and MES = ? ";
	private final static String SQL_DELETE_META_CUIDADO_DIURNO = "delete from dbo."+ CunamasConstants.TABLA_CUIDADO_DIURNO_META +" where ANIO = ? and MES = ? ";
	private final static String SQL_DELETE_META_QATARI = "delete from dbo."+ CunamasConstants.TABLA_QATARI_META +" where ANIO = ? and MES = ? ";
	private final static String SQL_DELETE_META_WWI = "delete from dbo."+ CunamasConstants.TABLA_WWI_META +" where ANIO = ? and MES = ? ";
	private final static String SQL_DELETE_META_SAF = "delete from dbo."+ CunamasConstants.TABLA_SAF_META +" where ANIO = ? and MES = ? ";
	private final static String SQL_DELETE_EJEC_QATARI = "delete from dbo."+ CunamasConstants.TABLA_QATARI_EJECUTADO +" where ANIO = ? and MES = ? ";
	private final static String SQL_DELETE_EJEC_WWI = "delete from dbo."+ CunamasConstants.TABLA_WWI_EJECUTADO +" where ANIO = ? and MES = ? ";
	
	private final static String SQL_INSERT_PRESUPUESTO_PROGRAMADO = "insert into dbo."+ CunamasConstants.TABLA_PRESUPUESTO_PROGRAMADO +"(ANIO,MES,FECCREA," +
											"DEP,PRO,INTERVENCIONES,PRESUPUESTO_PROGRAMADO,PRESUPUESTO_EJECUTADO) " +
										  	"values (?,?,?,?,?,?,?,?)";
	private final static String SQL_INSERT_META_CUIDADO_DIURNO = "insert into dbo."+ CunamasConstants.TABLA_CUIDADO_DIURNO_META +"(ANIO,MES,FECCREA," +
											"CODUBIGEO,DEP,PRO,DIS,NOMCOMGES,META_CUIDADO_DIURNO)" +
			  								"values (?,?,?,?,?,?,?,?,?)";
	private final static String SQL_INSERT_META_QATARI = "insert into dbo."+ CunamasConstants.TABLA_QATARI_META +"(ANIO,MES,FECCREA, " +
											"CODUBIGEO,DEP,PRO,DIS,META_QATARI)" +
											"values (?,?,?,?,?,?,?,?)";
	private final static String SQL_INSERT_META_WWI = "insert into dbo."+ CunamasConstants.TABLA_WWI_META +"(ANIO,MES,FECCREA, " +
											"CODUBIGEO,DEP,PRO,DIS,META_WWI)" +
			  								"values (?,?,?,?,?,?,?,?)";
	private final static String SQL_INSERT_META_SAF = "insert into dbo."+ CunamasConstants.TABLA_SAF_META +"(ANIO,MES,FECCREA, " +
											"CODUBIGEO,DEP,PRO,DIS,META_SAF)" +
			  								"values (?,?,?,?,?,?,?,?)";
	private final static String SQL_INSERT_EJEC_QATARI = "insert into dbo."+ CunamasConstants.TABLA_QATARI_EJECUTADO +"(ANIO,MES,FECCREA, " +
											"CODUBIGEO,DEP,PRO,DIS,EJEC_QATARI)" +
			  								"values (?,?,?,?,?,?,?,?)";
	private final static String SQL_INSERT_EJEC_WWI = "insert into dbo."+ CunamasConstants.TABLA_WWI_EJECUTADO +"(ANIO,MES,FECCREA, " +
											"CODUBIGEO,DEP,PRO,DIS,EJEC_WWI)" +
			  								"values (?,?,?,?,?,?,?,?)";
	
	@Autowired
	public void setJdbcTemplate(DataSource dataSource) {
		 this.jdbcTemplate = new JdbcTemplate(dataSource);
	}


	public void persistBudgetInputProgrammed(
			List<BudgetInputBean> budgetInputBeanList) {
		if(budgetInputBeanList==null) return;
		
		for (BudgetInputBean budgetInputBean : budgetInputBeanList) {

			jdbcTemplate.update(SQL_INSERT_PRESUPUESTO_PROGRAMADO, 
					new Object[] { budgetInputBean.getAnio(),budgetInputBean.getMes(), 
					new Date(),
					budgetInputBean.getDepartamento(),
					budgetInputBean.getProvincia(),
					budgetInputBean.getIntervenciones(),
					budgetInputBean.getPresupuestoTotalProgramado(),
					budgetInputBean.getPresupuestoTotalEjecutado()
					});
		}
	}


	public void deleteBudgetInputProgrammedByAnioAndMes(Long anio, Long mes) {
		jdbcTemplate.update(SQL_DELETE_PRESUPUESTO_PROGRAMADO, new Object[] {anio, mes});
	}


	public void deleteGoalsInputCuidadoDiurnoByAnioAndMes(Long anio, Long mes) {
		jdbcTemplate.update(SQL_DELETE_META_CUIDADO_DIURNO, new Object[] {anio, mes});
	}


	public void persistGoalsInputCuidadoDiurno(
			List<BudgetInputBean> goalsInputCuidadoDiurnoBeanList) {
		if(goalsInputCuidadoDiurnoBeanList==null) return;
		
		for (BudgetInputBean budgetInputBean : goalsInputCuidadoDiurnoBeanList) {

			jdbcTemplate.update(SQL_INSERT_META_CUIDADO_DIURNO, 
					new Object[] { budgetInputBean.getAnio(),budgetInputBean.getMes(), 
					new Date(),
					budgetInputBean.getCodigoUbigeo(),
					budgetInputBean.getDepartamento(),
					budgetInputBean.getProvincia(),
					budgetInputBean.getDistrito(),
					budgetInputBean.getNombreComiteGestion(),
					budgetInputBean.getMetaCuidadoDiurno()
					});
		}
	}


	public void deleteGoalsInputQatariByAnioAndMes(Long anio, Long mes) {
		jdbcTemplate.update(SQL_DELETE_META_QATARI, new Object[] {anio, mes});
	}


	public void persistGoalsInputQatari(
			List<BudgetInputBean> goalsInputQatariBeanList) {
		if(goalsInputQatariBeanList==null) return;
		
		for (BudgetInputBean budgetInputBean : goalsInputQatariBeanList) {

			jdbcTemplate.update(SQL_INSERT_META_QATARI, 
					new Object[] { budgetInputBean.getAnio(),budgetInputBean.getMes(), 
					new Date(),
					budgetInputBean.getCodigoUbigeo(),
					budgetInputBean.getDepartamento(),
					budgetInputBean.getProvincia(),
					budgetInputBean.getDistrito(),
					budgetInputBean.getMetaQatari()
					});
		}
	}


	public void deleteGoalsInputWwiByAnioAndMes(Long anio, Long mes) {
		jdbcTemplate.update(SQL_DELETE_META_WWI, new Object[] {anio, mes});
	}


	public void persistGoalsInputWwi(List<BudgetInputBean> goalsInputWwiBeanList) {
		if(goalsInputWwiBeanList==null) return;
		
		for (BudgetInputBean budgetInputBean : goalsInputWwiBeanList) {

			jdbcTemplate.update(SQL_INSERT_META_WWI, 
					new Object[] { budgetInputBean.getAnio(),budgetInputBean.getMes(), 
					new Date(),
					budgetInputBean.getCodigoUbigeo(),
					budgetInputBean.getDepartamento(),
					budgetInputBean.getProvincia(),
					budgetInputBean.getDistrito(),
					budgetInputBean.getMetaWwi()
					});
		}
	}


	public void deleteGoalsInputSafByAnioAndMes(Long anio, Long mes) {
		jdbcTemplate.update(SQL_DELETE_META_SAF, new Object[] {anio, mes});
	}


	public void persistGoalsInputSaf(List<BudgetInputBean> goalsInputSafBeanList) {
		if(goalsInputSafBeanList==null) return;
		
		for (BudgetInputBean budgetInputBean : goalsInputSafBeanList) {

			jdbcTemplate.update(SQL_INSERT_META_SAF, 
					new Object[] { budgetInputBean.getAnio(),budgetInputBean.getMes(), 
					new Date(),
					budgetInputBean.getCodigoUbigeo(),
					budgetInputBean.getDepartamento(),
					budgetInputBean.getProvincia(),
					budgetInputBean.getDistrito(),
					budgetInputBean.getMetaSaf()
					});
		}
	}


	public void deleteQatariInputByAnioAndMes(Long anio, Long mes) {
		jdbcTemplate.update(SQL_DELETE_EJEC_QATARI, new Object[] {anio, mes});
	}


	public void persistQatariInput(List<BudgetInputBean> qatariInputBeanList) {
		if(qatariInputBeanList==null) return;
		
		for (BudgetInputBean budgetInputBean : qatariInputBeanList) {

			jdbcTemplate.update(SQL_INSERT_EJEC_QATARI, 
					new Object[] { budgetInputBean.getAnio(),budgetInputBean.getMes(), 
					new Date(),
					budgetInputBean.getCodigoUbigeo(),
					budgetInputBean.getDepartamento(),
					budgetInputBean.getProvincia(),
					budgetInputBean.getDistrito(),
					budgetInputBean.getEjecutadoQatari()
					});
		}
	}


	public void deleteWwiInputByAnioAndMes(Long anio, Long mes) {
		jdbcTemplate.update(SQL_DELETE_EJEC_WWI, new Object[] {anio, mes});
	}


	public void persistWwiInput(List<BudgetInputBean> wwiInputBeanList) {
		if(wwiInputBeanList==null) return;
		
		for (BudgetInputBean budgetInputBean : wwiInputBeanList) {

			jdbcTemplate.update(SQL_INSERT_EJEC_WWI, 
					new Object[] { budgetInputBean.getAnio(),budgetInputBean.getMes(), 
					new Date(),
					budgetInputBean.getCodigoUbigeo(),
					budgetInputBean.getDepartamento(),
					budgetInputBean.getProvincia(),
					budgetInputBean.getDistrito(),
					budgetInputBean.getEjecutadoWwi()
					});
		}
	}
	
	
}
